package com.fms.controller;

import com.fms.result.ResultData;
import com.fms.queryData.ProfileUpdateQuery;
import com.fms.queryData.SysUserAdjustQuery;
import com.fms.entity.SysUser;
import com.fms.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
@RequiredArgsConstructor
public class SysUserController {

    private final SysUserService sysUserService;

    //获取员工信息
    @GetMapping("/getUsers/{id}")
    public ResultData getUser(@PathVariable("id") String id){
        return ResultData.success(sysUserService.getUserInfo(id));
    }

    //获取员工列表
    @GetMapping("/getAllUsers")
    public ResultData getAllUsers(){
        List<SysUser> sysUsers = sysUserService.getAllUsers();
        return ResultData.success(sysUsers);
    }

    //登录
    @PostMapping("/login/{username}/{password}")
    public ResultData login(@PathVariable("username") String username,
                            @PathVariable("password") String password){
        System.out.println(username+"="+password);
        String token = sysUserService.login(username, password);
        if (token.contains("Error")){
            int i = token.indexOf(":");
            String substring = token.substring(i + 1);
            return ResultData.error(substring);
        }
        return ResultData.success("login success",token);
    }

    //更新员工信息(useless)
    @PostMapping("/updateProfile")
    public ResultData updateProfile(@RequestBody ProfileUpdateQuery profileUpdateQuery){
        String info = sysUserService.updateProfile(profileUpdateQuery);
        return ResultData.success(info);
    }

    //更新员工权限
    @PostMapping("/adjustSysUser")
    public ResultData adjustSysUser(@RequestBody SysUserAdjustQuery sysUserAdjustQuery){
        System.out.println(sysUserAdjustQuery);
        String info = sysUserService.adjustSysUser(sysUserAdjustQuery);
        return ResultData.success(info);
    }

    //新增员工
    @PostMapping("/addSysUser")
    public ResultData addNewSysUser(@RequestBody SysUser sysUser){
        String info = sysUserService.addNewSysUser(sysUser);
        return ResultData.success(info);
    }

    //删除员工
    @PostMapping("/deleteSysUser")
    public ResultData deleteSysUser(@RequestBody SysUser[] sysUsers){
        System.out.println(sysUsers.length);
        String s = sysUserService.deleteSysUser(sysUsers);
        return ResultData.success(s);
    }
}
